package com.xinke.module.system.service.user;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import com.xinke.module.system.model.vo.user.*;
import com.xinke.module.system.model.entity.user.RoleMenuDO;
import com.xinke.component.common.pojo.PageResult;
import com.xinke.component.common.pojo.PageParam;
import com.xinke.component.common.util.object.BeanUtils;

import com.xinke.module.system.mapper.user.RoleMenuMapper;

import static com.xinke.component.common.exception.util.ServiceExceptionUtil.exception;
import static com.xinke.module.system.enums.ErrorCodeConstants.*;

/**
 * 角色和菜单关联 Service 实现类
 *
 * @author yzx
 */
@Service
@Validated
public class RoleMenuServiceImpl implements RoleMenuService {

    @Resource
    private RoleMenuMapper roleMenuMapper;

    @Override
    public Long createRoleMenu(RoleMenuSaveReqVO createReqVO) {
        // 插入
        RoleMenuDO roleMenu = BeanUtils.toBean(createReqVO, RoleMenuDO.class);
        roleMenuMapper.insert(roleMenu);
        // 返回
        return roleMenu.getRoleMenuId();
    }

    @Override
    public void updateRoleMenu(RoleMenuSaveReqVO updateReqVO) {
        // 校验存在
        validateRoleMenuExists(updateReqVO.getRoleMenuId());
        // 更新
        RoleMenuDO updateObj = BeanUtils.toBean(updateReqVO, RoleMenuDO.class);
        roleMenuMapper.updateById(updateObj);
    }

    @Override
    public void deleteRoleMenu(Long roleMenuId) {
        // 校验存在
        validateRoleMenuExists(roleMenuId);
        // 删除
        roleMenuMapper.deleteById(roleMenuId);
    }

    private void validateRoleMenuExists(Long roleMenuId) {
        if (roleMenuMapper.selectById(roleMenuId) == null) {
            throw exception(null);
        }
    }

    @Override
    public RoleMenuDO getRoleMenu(Long roleMenuId) {
        return roleMenuMapper.selectById(roleMenuId);
    }

    @Override
    public PageResult<RoleMenuDO> getRoleMenuPage(RoleMenuPageReqVO pageReqVO) {
        return roleMenuMapper.selectPage(pageReqVO);
    }

}